package com.threeox.commonlibrary.util.engine;

import android.content.Context;
import android.content.Intent;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.threeox.commonlibrary.entity.engine.event.BaseEventMessage;
import com.threeox.commonlibrary.entity.engine.event.EventMessage;
import com.threeox.commonlibrary.entity.engine.event.EventTypeMsg;
import com.threeox.commonlibrary.entity.engine.request.base.BaseRequestMsg;
import com.threeox.commonlibrary.inter.event.OnEventListener;
import com.threeox.commonlibrary.inter.OnRequestListener;
import com.threeox.commonlibrary.util.request.RequestHelper;
import com.threeox.utillibrary.util.EmptyUtils;
import com.threeox.utillibrary.util.ToastUtils;
import com.threeox.utillibrary.util.sys.ClipboardUtils;

import java.util.List;

/**
 *
 * @ClassName: EventFactory
 *
 * @Description: TODO(事件对象工厂类)
 *
 * @author 赵屈犇
 *
 * @date 创建时间: 2017/7/5 15:06
 *
 * @version 1.0
 *
 */
public class EventFactory {

    private Context mContext;
    private ViewGroup mViewGroup;
    private RequestHelper mRequestHelper;
    private OnEventListener mOnEventListener;

    private EventFactory(ViewGroup viewGroup, Context context) {
        this.mContext = context;
        this.mViewGroup = viewGroup;
        this.mRequestHelper = RequestHelper.newInstance();
    }

    public static EventFactory newInstance(ViewGroup viewGroup, Context context) {
        return new EventFactory(viewGroup, context);
    }

    /**
     * 加载事件对象集合
     *
     * @param eventMessages
     * @param intent
     */
    public void loadEventMessage(List<EventMessage> eventMessages, Intent intent) {
        if (EmptyUtils.isNotEmpty(eventMessages)) {
            for (final EventMessage eventMessage : eventMessages) {
                eventMessage.initAllParams(intent);
                View view = getView(eventMessage);
                if (view == null) {
                    continue;
                }
                String eventType = eventMessage.getEventType();
                if (EventTypeMsg.CLICK.equals(eventType)) {// 点击事件
                    view.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            execEventMessage(view, eventMessage);
                        }
                    });
                } else if (EventTypeMsg.TOUCH.equals(eventType)) {// 触摸事件
                    view.setOnTouchListener(new View.OnTouchListener() {
                        @Override
                        public boolean onTouch(View view, MotionEvent motionEvent) {
                            execEventMessage(view, eventMessage);
                            return false;
                        }
                    });
                }
            }
        }
    }

    /**
     * 动态执行
     *
     * @param view
     * @param baseEventMessage
     * @param params
     */
    public void execBaseEventMessage(View view, BaseEventMessage baseEventMessage, Object... params) {
        execEventMessage(view, convertEventMessage(baseEventMessage), params);
    }

    /**
     *
     * @param view
     * @param eventMessage
     * @param params
     */
    public void execEventMessage(View view, EventMessage eventMessage, Object... params) {
        boolean isExecute = true;
        if (mOnEventListener != null) {
            isExecute = mOnEventListener.onBeforeExecute(view, eventMessage, params);
        }
        if (isExecute) {
            execEventMessage(eventMessage, params);
            if (view != null && view instanceof TextView && eventMessage.isCopyContent()) {
                ClipboardUtils.copyText(mContext, ((TextView) view).getText());
                ToastUtils.showShortToast(mContext, "已成功复制内容!");
            }
        }
        if (mOnEventListener != null) {
            mOnEventListener.onAfterExecute(view, eventMessage, params);
        }
    }

    public void execEventMessage(final EventMessage eventMessage, Object... params) {
        if (null != eventMessage.getRequestMsg()) {
            mRequestHelper.setOnRequestListener(new OnRequestListener() {
                @Override
                public void onSuccess(BaseRequestMsg requestMsg, String url, int id, String msg, Object data) {
                    if (mOnEventListener != null) {
                        mOnEventListener.onEventSuccess(requestMsg, url, id, msg, data, eventMessage);
                    }
                }

                @Override
                public void onError(BaseRequestMsg requestMsg, String url, int id, Object code, String msg, Object data) {
                    if (mOnEventListener != null) {
                        mOnEventListener.onEventError(requestMsg, url, id, code, msg, eventMessage);
                    }
                }

                @Override
                public void inProgress(BaseRequestMsg requestMsg, float progress, long total, int id, String url) {
                    if (mOnEventListener != null) {
                        mOnEventListener.inEventProgress(requestMsg, progress, total, id, url, eventMessage);
                    }
                }
            }).execRequest(eventMessage.getRequestMsg());
        } else if (null != eventMessage.getActivityMsg()) {
            eventMessage.getActivityMsg().start(mContext);
        }
    }

    /**
     * 得到View
     *
     * @param eventMessage
     * @return
     */
    private View getView(EventMessage eventMessage) {
        if (eventMessage.getId() != null) {
            return mViewGroup.findViewById(eventMessage.getId());
        } else {
            return mViewGroup.findViewWithTag(eventMessage.getTag());
        }
    }

    /**
     * 转换为EventMessage对象
     *
     * @param baseEventMessage
     * @return
     */
    public EventMessage convertEventMessage(BaseEventMessage baseEventMessage) {
        EventMessage eventMessage = new EventMessage();
        eventMessage.setIsFinish(baseEventMessage.getIsFinish());
        eventMessage.setLoadText(baseEventMessage.getLoadText());
        eventMessage.setRequestMsg(baseEventMessage.getRequestMsg());
        eventMessage.setVerifyMsgs(baseEventMessage.getVerifyMsgs());
        eventMessage.setActivityMsg(baseEventMessage.getActivityMsg());
        eventMessage.setBroadCastMsgs(baseEventMessage.getBroadCastMsgs());
        return eventMessage;
    }

    public RequestHelper getRequestHelper() {
        return mRequestHelper;
    }

    public void setIntent(Intent intent) {
        mRequestHelper.setIntent(intent);
    }

    public void setOnEventListener(OnEventListener onEventListener) {
        this.mOnEventListener = onEventListener;
    }

}
